package com.cwl.common.exception;

import com.cwl.common.constant.ApiConstant;
import com.cwl.common.dto.UEResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * 全局的的异常拦截器（拦截所有的控制器）（带有@RequestMapping注解的方法上都会拦截）
 *
 * @author cwledit
 */
@ControllerAdvice
@Order(200)
@Slf4j
public class DefaultExceptionHandler {


    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public UEResult handleException(Exception e) {
        log.error(e.getMessage(), e);
        return UEResult.error(ApiConstant.CODE_FAIL, ExceptionUtils.getRootCauseMessage(e));
    }

    @ExceptionHandler(UEException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    @ResponseBody
    public UEResult handleUlinkException(UEException e) {
        e.printStackTrace();
        return UEResult.error(e.getCode(), e.getMsg());
    }

}
